Skip to content

ENT-6193, CFE-3421: Added policy function classfilterdata() #5836

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

larsewi
Copy link
Contributor

@larsewi larsewi commented Jul 3, 2025

  • Added policy function classfilterdata()
  • Added acceptance tests for classfilterdata

Ticket: ENT-6193, CFE-3421
Changelog: Title
Signed-off-by: Lars Erik Wik <lars.erik.wik@northern.tech>
@olehermanse
Copy link
Member

@cf-bottom jenkins, please

@cf-bottom
Copy link

cf-bottom commented Jul 4, 2025

Edit: This build is green, but parts of it was not run (DTs).

Sure, I triggered a build:

Build Status

Jenkins: https://ci.cfengine.com/job/pr-pipeline/12330/

Packages: http://buildcache.cfengine.com/packages/testing-pr/jenkins-pr-pipeline-12330/

@larsewi larsewi marked this pull request as ready for review July 14, 2025 16:05
@olehermanse
Copy link
Member

@cf-bottom please build in Jenkins again :)

@cf-bottom
Copy link

Ticket: ENT-10961, CFE-1840
Signed-off-by: Lars Erik Wik <lars.erik.wik@northern.tech>
@larsewi larsewi requested a review from craigcomstock July 15, 2025 14:58
Comment on lines +40 to +44
"expected[0]"
string => storejson('[
{ "key_0": "foo", "key_1": "!foo", "key_2": "foo&bar", "key_3": "foo|bar" },
{ "key_0": "bar", "key_1": "!bar", "key_2": "bar&baz", "key_3": "bar|baz" },
]');
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this whole test would be a lot easier to read (and verify that it's correct) if you did it in a more simple way - calling another bundle, using expandrange, etc. is really not necessary AFAICT. i.e.

  vars:
    "data"
      data => '[
        { "key_0": "foo", "key_1": "!foo", "key_2": "foo&bar", "key_3": "foo|bar" },
        { "key_0": "bar", "key_1": "!bar", "key_2": "bar&baz", "key_3": "bar|baz" },
        { "key_0": "baz", "key_1": "!baz", "key_2": "foo&baz", "key_3": "foo|baz" },
      ]';
    # Explain the test case:
    "expected[0]"
      string => storejson('[
        { "key_0": "foo", "key_1": "!foo", "key_2": "foo&bar", "key_3": "foo|bar" },
        { "key_0": "bar", "key_1": "!bar", "key_2": "bar&baz", "key_3": "bar|baz" },
      ]');
    "actual[0]"
      data => classfilterdata("@(data)", "array_of_objects", "key_0");
    # Check that actual[0] == expected[0]

foo and bar could also be replaced by classes which make it easier to read the data / test case, i.e.:

  classes:
    "class_exists";
    # "class_missing";

(or true / false).

Comment on lines +7793 to +7800
size_t index = strtoul(class_expr_index, &endptr, 10);
if (!StringEqual(endptr, "")) /* check that the whole string was consumed */
{
Log(LOG_LEVEL_VERBOSE,
"Function %s(): Bad class expression index '%s': Not a valid integer",
fn_name, class_expr_index);
return false;
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we have some functions for this StringToLong and StringToUnsignedLong maybe, did you check if you can use them?

Comment on lines +7944 to +7946
for (size_t i = JsonLength(parent); i > 0; i--)
{
size_t index = i - 1;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like this could easily turn into an off-by-one error, I'd consider being more explicit;

Suggested change
for (size_t i = JsonLength(parent); i > 0; i--)
{
size_t index = i - 1;
for (size_t index_plus_one = JsonLength(parent); index_plus_one > 0; index_plus_one--)
{
size_t index = index_plus_one - 1;

@olehermanse
Copy link
Member

@cf-bottom jenkins, please

@cf-bottom
Copy link

cf-bottom commented Jul 15, 2025

Lars had already done a rebuild so I cancelled tom's build and s/12359/12358/ here.

Build Status

Jenkins: https://ci.cfengine.com/job/pr-pipeline/12358/

Packages: http://buildcache.cfengine.com/packages/testing-pr/jenkins-pr-pipeline-12358/

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

4 participants